package com.budwk.app.sqfw.tencent;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.ocr.v20181119.OcrClient;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRRequest;
import com.tencentcloudapi.ocr.v20181119.models.GeneralBasicOCRResponse;
import com.tencentcloudapi.ocr.v20181119.models.TextDetection;
import org.nutz.img.Images;
import org.nutz.repo.Base64;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * 腾讯云图片识别
 *
 * AKIDtokYAY5V5Xmwta5NSPioeIwUt3xc2C7n
 * WpyzwhDVq58wZzCAsRn4QXR5RaXJsFPN
 */
public class TCtpsb {

    public static void main(String[] args) throws TencentCloudSDKException, IOException {
        String base64Image = Images.encodeBase64("D:\\接收文件\\创新\\互联网平台\\图像识别\\微信语音\\tianlv.jpg");
        ocrWxPic(base64Image);

    }

    /**
     * 识别微信通话用户名
     * @param base64Image base64图片
     * @return 返回用户名
     */
    public static String ocrWxPic(String base64Image) {
        GeneralBasicOCRResponse resp = startOcr(base64Image);
        byte[] b = Base64.decode(base64Image);
        ByteArrayInputStream in = new ByteArrayInputStream(b);

        BufferedImage image = null;
        try {
            image = ImageIO.read(in);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        TextDetection[] textDetections = resp.getTextDetections();

        String name = "";
        Long height = 0L;

        Long maxX = (long) image.getWidth();
        Long maxY = (long) image.getHeight();


        // 用户姓名，x一定是居中的， y一定不是偏离在上下的
        for (TextDetection detection : textDetections) {
            if (detection.getItemPolygon().getHeight() > height) {
                long mx = detection.getItemPolygon().getX() + (detection.getItemPolygon().getWidth()/2);
                long my = detection.getItemPolygon().getY() + (detection.getItemPolygon().getHeight()/2);
                //分为三份，左右的取消 | | |
                if (mx < (maxX /3) || mx > (maxX-maxX/3)) {
                    continue;
                }

                //分为4份，上下取消
                if (my < (maxY /4) || my > (maxY-maxY/3)) {
                    continue;
                }
                height = detection.getItemPolygon().getHeight();
                name = detection.getDetectedText();
            }
        }


        System.out.println("解析结果 name = " + name);
        return name;
    }

    /**
     * 腾讯云识别OCR
     * @param base64Image 图片base64
     * @return 腾讯云识别结果
     */
    public static GeneralBasicOCRResponse startOcr(String base64Image)  {
        // 实例化一个认证对象，入参需要传入腾讯云账户 SecretId 和 SecretKey，此处还需注意密钥对的保密
        // 代码泄露可能会导致 SecretId 和 SecretKey 泄露，并威胁账号下所有资源的安全性。以下代码示例仅供参考，建议采用更安全的方式来使用密钥，请参见：https://cloud.tencent.com/document/product/1278/85305
        // 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
        String secretID = "AKIDtokYAY5V5Xmwta5NSPioeIwUt3xc2C7n";
        String SecretKey = "WpyzwhDVq58wZzCAsRn4QXR5RaXJsFPN";
        Credential cred = new Credential(secretID, SecretKey);

        // 实例化一个http选项，可选的，没有特殊需求可以跳过
        HttpProfile httpProfile = new HttpProfile();
        httpProfile.setEndpoint("ocr.tencentcloudapi.com");
        // 实例化一个client选项，可选的，没有特殊需求可以跳过
        ClientProfile clientProfile = new ClientProfile();
        clientProfile.setHttpProfile(httpProfile);
        // 实例化要请求产品的client对象,clientProfile是可选的
        OcrClient client = new OcrClient(cred, "ap-guangzhou", clientProfile);
        // 实例化一个请求对象,每个接口都会对应一个request对象
        GeneralBasicOCRRequest req = new GeneralBasicOCRRequest();
        //设置OCR对应参数


        req.setImageBase64(base64Image);

        // 返回的resp是一个GeneralBasicOCRResponse的实例，与请求对象对应
        GeneralBasicOCRResponse resp = null;
        try {
            resp = client.GeneralBasicOCR(req);
        } catch (TencentCloudSDKException e) {
            throw new RuntimeException(e);
        }


        // 输出json格式的字符串回包
        System.out.println("腾讯云识别结果: "+GeneralBasicOCRResponse.toJsonString(resp));
        return resp;
    }
}
